import { getData, updateData } from '@vimi/utils-api'
import { isDefined } from '@vimi/utils-tool'
import { getApprove } from '@/credit/mixin/wf'

const relatedTableName = 'customer_related'
export const wfKey = 'PROJECT_APPROVAL_DIR'

export const enumGauFundsType = {
  1: '一次性收取',
  // 2: '按年收取',
}

// 评审委员会审批 0未审批
export const enumCommitteeType = {
  1: '同意',
  2: '否决',
  3: '复议',
  4: '有条件同意',
}

// 获取项目信息（关联表）
export const getRelated = relatedId =>
  getData({
    table: relatedTableName,
    params: {
      'customer_related_id.eq': relatedId,
    },
  }).then(res => res?.data?.[0] ?? {})

// 更新关联表信息
export const updateRelatedData = (id, data) =>
  updateData({
    table: relatedTableName,
    id,
    data,
  })

// 判断是否为企业
export const projectIsCompany = projectData => +projectData.customer_type === 1

const approveConfig = {
  'PROJECT_APPROVAL_DIR-RESA': {
    cmpt: 'approve-related',
    rekey: null,
  },
  'PROJECT_APPROVAL_DIR-RISK': {
    cmpt: 'approve-risk',
    rekey: 'PROJECT_APPROVAL_DIR-RESA',
  },
  'PROJECT_APPROVAL_DIR-RISK_MANAGE': {
    cmpt: 'approve-risk-manager',
    rekey: 'PROJECT_APPROVAL_DIR-RESA',
  },
  'PROJECT_APPROVAL_DIR-JURY': {
    cmpt: 'approve-committee',
    rekey: null,
    type: 1,
  },
  'PROJECT_APPROVAL_DIR-SECRETART': {
    cmpt: 'approve-register',
    rekey: null,
    type: [1, 2],
    dataParse: () => ({
      type: 2,
    }),
  },
}

// 获取审批流程配置信息
export const getApproveProcess = (related, user) => {
  const pType = +(related.review_method || '')
  return getApprove(
    {
      wfKey,
      queryId: related.customer_related_id,
      config: approveConfig,
      next: processItem => {
        const { type } = approveConfig[processItem.wfn_key] || {}
        if (isDefined(type)) {
          const isMatchType = Array.isArray(type) ? type.includes(pType) : pType === type
          return isMatchType
        }
        return true
      },
    },
    user
  )
}
